package com.oig.sys.base.service.impl;

import com.oig.common.exception.BadRequestException;
import com.oig.sys.base.dao.ITbHsGoodsDao;
import com.oig.sys.base.dao.ITbHsGoodsTariffDao;
import com.oig.sys.base.dto.HsGoodsTariffDto;
import com.oig.sys.base.entity.TbHsGoods;
import com.oig.sys.base.entity.TbHsGoodsTariff;
import com.oig.sys.base.service.ITbHsGoodsTariffService;
import com.oig.sys.base.vo.HsGoodsTariffVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
public class TbHsGoodsTariffServiceImpl implements ITbHsGoodsTariffService {

	private final ITbHsGoodsTariffDao tbHsGoodsTariffDao;
	private final ITbHsGoodsDao hsGoodsDao;

	@Autowired
	public TbHsGoodsTariffServiceImpl(ITbHsGoodsTariffDao tbHsGoodsTariffDao,ITbHsGoodsDao hsGoodsDao){
		 this.tbHsGoodsTariffDao = tbHsGoodsTariffDao;
		 this.hsGoodsDao = hsGoodsDao;
	}


	@Transactional
	@Override
	public HsGoodsTariffVo addOrUpdateTariff(HsGoodsTariffDto hsGoodsTariffDto) {
		TbHsGoodsTariff tariff = this.tbHsGoodsTariffDao.getByCountryCodeAndHsCode(hsGoodsTariffDto.getCountryCode(), hsGoodsTariffDto.getHsCode()) ;
		if (tariff!=null){
			if (hsGoodsTariffDto.getPriority().compareTo(tariff.getPriority())<0) {		// 如果优先级低不更新
				log.debug("priority n<o , n={} o={}", hsGoodsTariffDto.getPriority(), tariff.getPriority());
				return this.buildVo(tariff);
			}
			if (hsGoodsTariffDto.getPriority().compareTo(tariff.getPriority())==0){//优先级一样
				if(hsGoodsTariffDto.getTariffRate().compareTo(tariff.getTariffRate()) > 0){	//保留小的
					log.debug("tariffRate n>o, n={}, o={}", hsGoodsTariffDto.getTariffRate(), tariff.getTariffRate()); //旧的大于新的不更新
					return this.buildVo(tariff);
				}
//				if (hsGoodsTariffDto.getTariffRate().compareTo(tariff.getTariffRate()) == 0 && hsGoodsTariffDto.getTariffType().equals(tariff.getTariffType())){
//					//税率也一样时看有没有 税率方式 //税率方式也一样，那就不保存了
//					log.debug("tariffRate and tariffType same.");
//					return this.buildVo(tariff);
//				}
			}
			tariff.setPriority(hsGoodsTariffDto.getPriority());
			tariff.setCountryApplyCode(hsGoodsTariffDto.getCountryApplyCode());
			tariff.setCountryName(hsGoodsTariffDto.getCountryName());
			tariff.setTariffType(hsGoodsTariffDto.getTariffType());
			tariff.setTariffRate(hsGoodsTariffDto.getTariffRate());
			tariff.setRemark(hsGoodsTariffDto.getRemark());
			tariff.setLastUpdateId(hsGoodsTariffDto.getLastUpdateId());
			tariff.setLastUpdateName(hsGoodsTariffDto.getLastUpdateName());
			tariff.setLastUpdateTime(new Date());
		} else {
			tariff = new TbHsGoodsTariff();
			BeanUtils.copyProperties(hsGoodsTariffDto, tariff);
			tariff.setCreateTime(new Date());
			tariff.setLastUpdateTime(tariff.getCreateTime());
		}
		this.tbHsGoodsTariffDao.save(tariff);
		return this.buildVo(tariff);
	}

	@Transactional
	@Override
	public HsGoodsTariffVo addHsGoodsTariff(HsGoodsTariffDto hsGoodsTariffDto) {
		TbHsGoodsTariff old = this.tbHsGoodsTariffDao.getByCountryCodeAndHsCode(hsGoodsTariffDto.getCountryCode(), hsGoodsTariffDto.getHsCode()) ;
		if (old!=null){
			throw new BadRequestException("国家已有优惠税率") ;
		}
		TbHsGoodsTariff hsGoodsTariff = new TbHsGoodsTariff();
		BeanUtils.copyProperties(hsGoodsTariffDto, hsGoodsTariff);
		this.tbHsGoodsTariffDao.save(hsGoodsTariff) ;
		return this.buildVo(hsGoodsTariff);
	}

	private HsGoodsTariffVo buildVo(TbHsGoodsTariff hsGoodsTariff){
		HsGoodsTariffVo hsGoodsTariffVo = new HsGoodsTariffVo();
		BeanUtils.copyProperties(hsGoodsTariff, hsGoodsTariffVo);
		return hsGoodsTariffVo;
	}

	@Transactional
	@Override
	public HsGoodsTariffVo updateHsGoodsTariff(HsGoodsTariffDto hsGoodsTariffDto) {
		TbHsGoodsTariff old = this.tbHsGoodsTariffDao.getByCountryCodeAndHsCode(hsGoodsTariffDto.getCountryCode(), hsGoodsTariffDto.getHsCode()) ;
		if (old!=null && !old.getId().equals(hsGoodsTariffDto.getId())){
			throw new BadRequestException("国家已有优惠税率") ;
		}

		TbHsGoodsTariff hsGoodsTariff = this.tbHsGoodsTariffDao.getGoodsTariffById(hsGoodsTariffDto.getId()) ;
		if (hsGoodsTariff==null){
			throw new BadRequestException("对应商品税率配置不存在");
		}
		BeanUtils.copyProperties(hsGoodsTariffDto, hsGoodsTariff, "id","hsCode");
		this.tbHsGoodsTariffDao.save(hsGoodsTariff) ;
		return this.buildVo(hsGoodsTariff);
	}

	@Transactional
	@Override
	public String delHsGoodsTariff(Integer id) {
		TbHsGoodsTariff hsGoodsTariff = this.tbHsGoodsTariffDao.getGoodsTariffById(id);
		if (hsGoodsTariff==null){
			throw new BadRequestException("对应商品税率配置不存在");
		}
		String code = hsGoodsTariff.getHsCode();
		this.tbHsGoodsTariffDao.delete(hsGoodsTariff);
		return code;
	}


	@Override
	public HsGoodsTariffVo getTariff(String countryCode, String hsCode) {
		HsGoodsTariffVo tariffVo = new HsGoodsTariffVo();
		TbHsGoods hsGoods = this.hsGoodsDao.getByHsCode(hsCode);
		if (hsGoods==null) throw new BadRequestException("hsCode错误");
		tariffVo.setCountryCode(countryCode);
		tariffVo.setHsCode(hsCode);
		tariffVo.setTariffType(hsGoods.getTariffType());
		if (hsGoods.getOffTariffRate()!=null){
			if (hsGoods.getProvTaxRate()!=null && hsGoods.getProvTaxRate().compareTo(hsGoods.getOffTariffRate())<0){
				tariffVo.setTariffRate(hsGoods.getProvTaxRate());
			} else {
				tariffVo.setTariffRate(hsGoods.getOffTariffRate());
			}
		} else {
			tariffVo.setTariffRate(hsGoods.getCommTariffRate());
		}
		tariffVo.setVatRate(hsGoods.getVatRate());
		tariffVo.setPriority(0);
//		tariffVo.setCmpTaxRate(hsGoods.getCmpTaxRate());
//		tariffVo.setExportRate(hsGoods.getExportRate());
//		tariffVo.setRebateRate(hsGoods.getRebateRate());
		tariffVo.setRemark(hsGoods.getRemark());
		TbHsGoodsTariff hsGoodsTariff = this.tbHsGoodsTariffDao.getByCountryCodeAndHsCode(countryCode,hsCode);
		if (hsGoodsTariff!=null){
			tariffVo.setId(hsGoodsTariff.getId());
			tariffVo.setCountryName(hsGoodsTariff.getCountryName());
			if (tariffVo.getTariffRate()==null||hsGoodsTariff.getPriority()>0){
				if (StringUtils.isNotBlank(hsGoodsTariff.getTariffType())){
					tariffVo.setTariffType(hsGoodsTariff.getTariffType());
				}
				tariffVo.setTariffRate(hsGoodsTariff.getTariffRate());
			} else {
				if (hsGoodsTariff.getTariffRate().compareTo(tariffVo.getTariffRate())<0){	//hsGoodsTariff.getTariffRate() 一定有值
					if (StringUtils.isNotBlank(hsGoodsTariff.getTariffType())){
						tariffVo.setTariffType(hsGoodsTariff.getTariffType());
					}
					tariffVo.setTariffRate(hsGoodsTariff.getTariffRate());
				}
			}
			tariffVo.setRemark(hsGoodsTariff.getRemark());
			tariffVo.setCountryApplyCode(hsGoodsTariff.getCountryApplyCode());
		}
		return tariffVo;
	}

	@Override
	public List<HsGoodsTariffVo> getHsGoodsAllTariff(String hsCode) {
		List<HsGoodsTariffVo> voList = new ArrayList<>();
		List<TbHsGoodsTariff> hsGoodsTariffList = this.tbHsGoodsTariffDao.findByHsCode(hsCode);
		if (hsGoodsTariffList!=null){
			hsGoodsTariffList.forEach((hsGoodsTariff)->voList.add(buildVo(hsGoodsTariff)));
		}
		return voList;
	}

	@Override
	public HsGoodsTariffVo getHsGoodsTariffInfoById(Integer id) {
		TbHsGoodsTariff hsGoodsTariff = tbHsGoodsTariffDao.getGoodsTariffById(id);
		return hsGoodsTariff == null ? null : this.buildVo(hsGoodsTariff);
	}

	/**
	 * 网站数据不全先不做删除
	 */
	@Transactional
	@Override
	public void reSetAllHsGoodsTariff(String hsCode, List<HsGoodsTariffDto> hsGoodsTariffDtoList) {
		log.debug("reSetAllHsGoodsTariff :{}", hsGoodsTariffDtoList);
		List<Integer> allListId = new ArrayList<>();
		for (HsGoodsTariffDto hsGoodsTariffDto : hsGoodsTariffDtoList){
			HsGoodsTariffVo hsGoodsTariffVo = this.addOrUpdateTariff(hsGoodsTariffDto);
			allListId.add(hsGoodsTariffVo.getId());
		}
		List<TbHsGoodsTariff> dellList = this.tbHsGoodsTariffDao.findByHsCodeExIdList(hsCode, allListId);
		if (dellList!=null && !dellList.isEmpty()){
			this.tbHsGoodsTariffDao.deleteAll(dellList);
		}
	}
}
